<!DOCTYPE html>
<html>
<head lang="en">
	<meta charset="utf-8"/>
	<title>个人信息</title>
	<link rel="stylesheet" type="text/css" href="css/public.css"/>
	<link rel="stylesheet" type="text/css" href="css/mygxin.css"/>
	<script src="js/jquery-1.12.4.min.js"></script>
	<script src="X-admin/lib/layui/layui.js"></script>
	<script src="X-admin/js/xadmin.js"></script>
	<link rel="stylesheet" href="X-admin/css/xadmin.css">
	<link rel="stylesheet" href="X-admin/lib/layui/css/layui.css">
</head>
<body>
<div class="layui-row">
	<form class="layui-form">

		<!---------------------工位名称-------------------->

		<div class="layui-form-item">
			<label class="layui-form-label">
				<span class="x-red">*</span>工位名称</label>
			<div class="layui-input-inline">
				<input type="text" id="stname" name="stname" required="" autocomplete="off" class="layui-input">
			</div>
		</div>

		<!---------------------工位单价---------------------->

		<div class="layui-form-item">
			<label class="layui-form-label">
				<span class="x-red">*</span>工位单价</label>
			<div class="layui-input-inline">
				<input type="text" id="prices" name="prices" required="" autocomplete="off" class="layui-input">
			</div>
		</div>

		<!--------------------工位场地内容介绍------------------->

		<div class="layui-form-item">
			<label class="layui-form-label">
				<span class="x-red">*</span>工位场地内容介绍</label>
			<div class="layui-input-inline">
				<input class="layui-input" autocomplete="off" name="conten" id="conten" required=""></div>
		</div>

		<!-------------------------工位类型----------------------->

		<div class="layui-form-item">
			<label class="layui-form-label">
				<span class="x-red">*</span>工位类型</label>
			<div class="layui-input-inline">
				<label for="sttype">
					<select id="sttype" name="sttype">
						<option value="工位">工位</option>
						<option value="会议厅">会议厅</option>
						<option value="路演厅">路演厅</option>
					</select>
				</label>
			</div>
		</div>

		<!-------------------------地址----------------------->

		<div class="layui-form-item">
			<label class="layui-form-label">选择地区</label>
			<div class="layui-input-inline">
				<select  id="provinceSelect" name="sheng" lay-filter="province">
					<option value="" id="1" name="sheng" selected="selected"></option>
				</select>
			</div>
			<div class="layui-input-inline">
				<select id="areaSelect" name="shi" lay-filter="area">
					<option value="" id="2" name="shi" selected="selected"></option>
				</select>
			</div>
			<div class="layui-input-inline">
				<select name="addrid" id="countySelect" lay-filter="county">
					<option value="" id="3" name="addrid" selected="selected"></option>
				</select>
			</div>
		</div>

		<!-------------------------详细地址---------------------->

		<div class="layui-form-item">
			<label class="layui-form-label">
				<span class="x-red">*</span>详细地址</label>
			<div class="layui-input-inline">
				<input class="layui-input" autocomplete="off" name="addres" id="addres" required=""></div>
		</div>

		<!------------------------场地面积----------------------->

		<div class="layui-form-item">
			<label class="layui-form-label">
				<span class="x-red">*</span>场地面积</label>
			<div class="layui-input-inline">
				<input class="layui-input" autocomplete="off" name="starea" id="starea" required=""></div>
		</div>

		<!-------------------------状态--------------------------->

		<div class="layui-form-item">
			<label class="layui-form-label">状态</label>
			<div class="layui-input-block">
				<label>
					<input type="radio" name="enable" value="1" title="开启">
				</label>
				<label>
					<input type="radio" name="enable" value="0" title="关闭">
				</label>
			</div>
		</div>

		<!-------------------------图片--------------------------->

		<div class="layui-form-item">
			<label for="imgHref" class="layui-form-label">
				<span class="x-red">*</span>图片</label>
			<div class="layui-input-inline">
				<button type="button" class="layui-btn" id="imgHref">
					<i class="layui-icon">&#xe67c;</i>上传图片
				</button>
			</div>
			<div class="layui-form-mid layui-word-aux"><img src="#" id="showImg" width="200" height="200" alt=""></div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label"></label>
			<button class="layui-btn" lay-filter="edit" lay-submit="submit">确认修改</button>
		</div>
	</form>

</div>
</body>

<script>

	var Address_Prefix_Lessors = "http://localhost:9200/api-lessors";

	$(function () {
		start();
		upload();
		// linkage();
	});

	function start() {
		layui.use(['form','upload'],function () {
			var form = layui.form;
			//得到工位数据
			var data = getStationData();
			console.log(data,"查到的数据");
			//数据赋值
			$("#stname").val(data.stname);  //名字
			$("#prices").val(data.prices);	//价格
			$("#conten").val(data.conten);	//内容

			$("#sttype option[value='"+data.sttype+"']").attr("selected","selected");	//工位类型

			$("#addres").val(data.addres);	//详细地址
			$("#starea").val(data.starea);	//面积
			$("#showImg").attr("src",data.topimg);	//图片回显

			$("input[name=enable][value="+data.enable+"]").attr("checked",true);  //状态


			form.render();

			console.log("11111:");
			getAddress(data);	//地址
			console.log("22222:");
			//编辑数据
			form.on('submit(edit)',function (data) {
				console.log("33333:",data);
				var data1 = data.field;
				data1.statid = sessionStorage.getItem("statid");
				data1.busiid = sessionStorage.getItem("busiid");
				data1.topimg = $("#showImg").attr("src");
				$.ajax({
					url: Address_Prefix_Lessors + '/business/station/update',//地址：访问后台的地址
					data: data1,//前端传递给后台的数据
					type: 'post',
					async: false,
					dataType: 'json',//是后端传递到前端的数据格式
					success: function (data) {//后端给前端的一个反馈
						console.log(data);
						if (data.code === 0) {
							layer.msg("ok！", {time: 1000}, function () {
								location.reload();
							})
						} else {
							layer.msg("Not ok！", {time: 1000})
						}
					}
				});
				return false;
			});
		})
	}
	//获取当前商家点击编辑工位数据
	function getStationData() {
		var result = [];
		$.ajax({
			url: Address_Prefix_Lessors + "/business/station/findById",
			data: {statid:sessionStorage.getItem("statid")},
			type: 'get',
			dataType: 'json',
			async : false,
			success: function (data) {
				console.log("工位数据",data.data);
				result = data.data;
				return result;
			}
		});
		return result;
	}
	//上传图片
	function upload() {
	layui.use('upload', function () {
		var upload = layui.upload;
		upload.render({
			elem: '#imgHref',//绑定元素
			url: Address_Prefix_Lessors + '/upload/file',//上传到后端接口
			done: function (res) {//上传完毕回调
				console.log(res);
				console.log(res.data);
				$("#showImg").attr("src", res.data);
			},
			error: function () {
				console.log("回调出错了!")
				//请求异常回调
			}
		})
	})
}
	//三级联动                                           // function linkage(){
	layui.use(['form','layer','jquery'],function(){
		var form = layui.form,
				layer = parent.layer === undefined ? layui.layer : parent.layer,
				$ = layui.jquery;

		var provinceText = "";
		var cityText = "";
		var areaText = "";
		//异步加载下拉框数据
		$.get(Address_Prefix_Lessors+"/business/address/GetAllProvince",{},function (data) {
			console.log("省数据：",data.data);
			if(data.code!==0){
				layer.msg(data.msg)
			}else{
				var html = "";
				if(data.data != null) {
					$.each(data.data, function (index, item) {
						html += "<option value='" + item.regiid + "'>" + item.ccname + "</option>";
					});
					$("#provinceSelect").append(html);

					// $("#provinceSelect").val("4");  //ggggggggggggggggggggggggggggggggggggggggggggggggggggggggg

					//append后必须从新渲染
					form.render('select');
				}
			}
		});

		//监听省下拉框
		form.on('select(province)', function(dataObj){
			//移除城市下拉框所有选项
			$("#areaSelect").empty();
			var cityHtml = '<option value="">请选择或搜索市</option>';
			$("#areaSelect").html(cityHtml);
			var areaHtml = '<option value="">请选择或搜索县/区</option>';
			$("#countySelect").html(areaHtml);

			provinceText = $("#provinceSelect").find("option:selected").text();
			var value = $("#provinceSelect").val();
			//异步加载下拉框数据
			$.get(Address_Prefix_Lessors+"/business/address/GetAreaByID",{"id":value},function (data) {
				console.log("市数据：",data.data);
				if(data.code!==0){
					layer.msg(data.msg)
				}else{
					var html = "";
					if(data.data != null) {
						$.each(data.data, function (index, item) {
							html += "<option value='" + item.regiid + "'>" + item.ccname + "</option>";
						});
						$("#areaSelect").append(html);
						//append后必须从新渲染
						form.render('select');
					}
				}
			});
		});

		//监听市下拉框
		form.on('select(area)', function(dataObj){
			//移除县区下拉框所有选项
			$("#countySelect").empty();
			var html = '<option value="">请选择或搜索县/区</option>';
			$("#countySelect").html(html);

			cityText = $("#areaSelect").find("option:selected").text();
			var value = $("#areaSelect").val();
			//异步加载下拉框数据
			$.get(Address_Prefix_Lessors+"/business/address/GetCountyByID",{"id":value},function (data) {
				console.log("区数据：",data.data);
				if(data.code!==0){
					layer.msg(data.msg)
				}else{
					var html = "";
					if(data.data != null) {
						$.each(data.data, function (index, item) {
							html += "<option value='" + item.regiid + "'>" + item.ccname + "</option>";
						});
						$("#countySelect").append(html);
						//append后必须从新渲染
						form.render('select');
					}
				}
			});
		});

		//监听县区下拉框
		form.on('select(county)', function(dataObj){
			areaText = $("#countySelect").find("option:selected").text();
		});
	});// }
	//获取地址
	function getAddress(data) {
		layui.use('form',function () {
			var form = layui.form;
			//获取省
			$.ajax({
				url: Address_Prefix_Lessors + "/business/address/GetProvinceByUserID",
				data: {id:data.addrid},
				type: 'get',
				dataType: 'json',
				async : false,
				success: function (data) {
					console.log("之前选的省：",data.data[0].ccname);
					$("#1").append(data.data[0].ccname);
					$("#1").val(data.data[0].regiid);

					// $("#provinceSelect").val(data.data[0].ccname);
					// $("#1").find("option[value='"+data.data[0].pareId+"']").attr("selected",true);
					form.render();
				}
			});
			//获取市
			$.ajax({
				url: Address_Prefix_Lessors + "/business/address/GetAreaByUserID",
				data: {id:data.addrid},
				type: 'get',
				dataType: 'json',
				async : false,
				success: function (data) {
					console.log("之前选的市：",data.data[0].ccname);
					$("#2").append(data.data[0].ccname);
					$("#2").val(data.data[0].regiid);
					form.render();
				}
			});
			//获取区
			$.ajax({
				url: Address_Prefix_Lessors + "/business/address/GetCountyByUserID",
				data: {id:data.addrid},
				type: 'get',
				dataType: 'json',
				async : false,
				success: function (data) {
					console.log("之前选的区：",data.data[0].ccname);
					$("#3").append(data.data[0].ccname);
					$("#3").val(data.data[0].regiid);
					form.render();
				}
			});
			return false;
		});
	}





	
</script>
</html>
